package com.shengsiyuan.nio;

import java.nio.ByteBuffer;

/**
 * 只读buffer，我们可以随时将一个普通的buffer调用asReadOnlyBuffer方法返回一个只读buffer
 * 但不能将一个只读buffer转换为读写buffer
 */
public class NioTest07 {

    public static void main(String[] arg) {

        ByteBuffer buffer = ByteBuffer.allocate(10);

        System.out.println(buffer.getClass());

        for (int i = 0; i < buffer.capacity(); i++) {
            buffer.put((byte) i);
        }

        ByteBuffer readonly = buffer.asReadOnlyBuffer(); // 只读 buffer，只能读不能写

        System.out.println(readonly.getClass());

        readonly.position(0);
        readonly.put((byte) 2);
    }
}
